package com.stn.zjx.givingtreasure.activity.common;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.DrawableTransformation;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.orhanobut.logger.Logger;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.ViewPagerAdpater;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.wingsofts.dragphotoview.DragPhotoView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;

public class DropImgsShowActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    BGABanner viewpager;
    int mOriginLeft = 0;
    int mOriginTop = 0;
    int mOriginHeight = 0;
    int mOriginWidth = 0;
    int mOriginCenterX = 0;
    int mOriginCenterY = 0;
    private float mTargetHeight = 0F;
    private float mTargetWidth = 0F;
    private float mScaleX = 0F;
    private float mScaleY = 0F;
    private float mTranslationX = 0F;
    private float mTranslationY = 0F;
    private ViewPagerAdpater dropadpater =null;
    private int imgposiiton=0;
    List<View> rootList=null;
    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        imgposiiton =getIntent().getIntExtra("position", 0);
        ArrayList<String> lists=getIntent().getStringArrayListExtra("imgs");
        if(lists==null||lists.size()==0){
            return;
        }
        rootList=initListData(lists);
        viewpager.setData(rootList);
        viewpager.setCurrentItem(imgposiiton);
        viewpager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewpager.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mOriginLeft = getIntent().getIntExtra("left", 0);
                mOriginTop = getIntent().getIntExtra("top", 0);
                mOriginHeight = getIntent().getIntExtra("height", 0);
                mOriginWidth = getIntent().getIntExtra("width", 0);
                mOriginCenterX = mOriginLeft + mOriginWidth / 2;
                mOriginCenterY = mOriginTop + mOriginHeight / 2;
                int[]  location = new int[2];
                DragPhotoView photoView = rootList.get(viewpager.getCurrentItem()).findViewById(R.id.drop_photo_view);
                photoView.getLocationOnScreen(location);
                mTargetHeight = photoView.getHeight();
                mTargetWidth = photoView.getWidth();
                mScaleX = mOriginWidth / mTargetWidth;
                mScaleY = mOriginHeight / mTargetHeight;
                float targetCenterX = location[0] + mTargetWidth / 2;
                float targetCenterY = location[1] + mTargetHeight / 2;
                mTranslationX = mOriginCenterX - targetCenterX;
                mTranslationY = mOriginCenterY - targetCenterY;
                photoView.setTranslationX(mTranslationX);
                photoView.setTranslationY( mTranslationY);
                photoView.setScaleX(mScaleX);
                photoView.setScaleY(mScaleY);
                performEnterAnimation(photoView);
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drop_img_show);
        StatusBarUtil.setImmersiveStatusBar(this,false);
        ButterKnife.bind(this);
        initComponent();
    }


    private List<View> initListData(ArrayList<String> datas){
        List<View> arlist = new ArrayList<>();
        for(String data: datas){
            System.out.println("data:"+data);
            View rootView=View.inflate(this, R.layout.item_dragphoto,null );
            DragPhotoView dragView=rootView.findViewById(R.id.drop_photo_view);
            ProgressBar ivLoading=rootView.findViewById(R.id.iv_loading);
            TextView loading_status=rootView.findViewById(R.id.loading_status);
            LinearLayout loading_layout=rootView.findViewById(R.id.loading_layout);
            loading_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    loadingImageView(dragView,data,loading_layout,ivLoading,loading_status);
                }
            });
            loading_layout.setClickable(false);
            loadingImageView(dragView,data,loading_layout,ivLoading,loading_status);


            dragView.setOnTapListener(new DragPhotoView.OnTapListener() {
                @Override
                public void onTap(DragPhotoView view) {

                }
            });
            dragView.setOnExitListener(new DragPhotoView.OnExitListener() {
                @Override
                public void onExit(DragPhotoView view, float translateX, float translateY, float w, float h) {
                    performExitAnimation(view, translateX, translateY);
                }
            });
            arlist.add(rootView);

        }
        return arlist;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
       if(viewpager!=null){
           finishWithAnimation((DragPhotoView) rootList.get(viewpager.getCurrentItem()).findViewById(R.id.drop_photo_view));
       }

    }

    //结束动画
    private void finishWithAnimation(final DragPhotoView photoView) {
        ValueAnimator translateXAnimator = ValueAnimator.ofFloat(0F, mTranslationX);
        translateXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setX((Float) animation.getAnimatedValue());
            }
        });
        translateXAnimator.setDuration(300);
        translateXAnimator.start();

        ValueAnimator translateYAnimator = ValueAnimator.ofFloat(0F, mTranslationY);
        translateYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setY((Float) animation.getAnimatedValue());
            }
        });

        ValueAnimator scaleYAnimator = ValueAnimator.ofFloat(1F, mScaleY);
        scaleYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setScaleY((float) animation.getAnimatedValue());
            }
        });
        scaleYAnimator.setDuration(300);
        scaleYAnimator.start();


        ValueAnimator scaleXAnimator = ValueAnimator.ofFloat(1F, mScaleX);
        scaleXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setScaleX((float) animation.getAnimatedValue());
            }
        });
        scaleXAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                animation.removeAllListeners();
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        scaleXAnimator.setDuration(300);
        scaleXAnimator.start();




    }
    private void performExitAnimation(final DragPhotoView view, float x, float y) {
        view.finishAnimationCallBack();
        float viewX = mTargetWidth / 2 + x - mTargetWidth * mScaleX / 2;
        float viewY = mTargetHeight / 2 + y - mTargetHeight * mScaleY / 2;
        view.setX(viewX);
        view.setY(viewY);
        float centerX = view.getX() + mOriginWidth / 2;
        float centerY = view.getY() + mOriginHeight / 2;
        float translateX = mOriginCenterX - centerX;
        float translateY = mOriginCenterY - centerY;
        ValueAnimator translateXAnimator = ValueAnimator.ofFloat(view.getX(), view.getX() + translateX);
        translateXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                view.setX((Float) animation.getAnimatedValue());
            }
        });
        translateXAnimator.setDuration(300);
        translateXAnimator.start();

        ValueAnimator translateYAnimator = ValueAnimator.ofFloat(view.getY(), view.getY() + translateY);
        translateYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                view.setY((Float) animation.getAnimatedValue());
            }
        });
        translateYAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                animation.removeAllListeners();
                finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        translateYAnimator.setDuration(300);
        translateYAnimator.start();
    }


    private void performEnterAnimation(DragPhotoView dragPhotoView) {
        final DragPhotoView photoView = dragPhotoView;
        ValueAnimator translateXAnimator =ValueAnimator.ofFloat(photoView.getX(), 0F);
        translateXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setX((float) animation.getAnimatedValue());
            }
        });
        translateXAnimator.setDuration(300);
        translateXAnimator.start();

        ValueAnimator translateYAnimator = ValueAnimator.ofFloat(photoView.getY(), 0F);
        translateYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setY((float) animation.getAnimatedValue());
            }
        });
        translateYAnimator.setDuration(300);
        translateYAnimator.start();

        ValueAnimator scaleYAnimator = ValueAnimator.ofFloat(mScaleY, 1F);
        scaleYAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setScaleY((float) animation.getAnimatedValue());
            }
        });
        scaleYAnimator.setDuration(300);
        scaleYAnimator.start();
        ValueAnimator scaleXAnimator = ValueAnimator.ofFloat(mScaleX, 1F);
        scaleXAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                photoView.setScaleX((float) animation.getAnimatedValue());
            }
        });
        scaleXAnimator.setDuration(300);
        scaleXAnimator.start();
    }

    public void loadingImageView(DragPhotoView dragPhotoView,String data,LinearLayout loading_layout,ProgressBar ivLoading,
                                TextView loading_status ){
        Glide
                .with(this)
                .load(data)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        loading_layout.setClickable(false);
                        loading_layout.setVisibility(View.GONE);
                        ivLoading.setVisibility(View.GONE);
                        dragPhotoView.setImageDrawable(resource);
                    }
                    @Override
                    public void onLoadStarted(@Nullable Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        loading_layout.setClickable(false);
                        loading_layout.setVisibility(View.VISIBLE);
                        ivLoading.setVisibility(View.GONE);
                        loading_status.setVisibility(View.VISIBLE);
                        loading_status.setText(getString(R.string.loading));

                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                        loading_layout.setClickable(true);
                        ivLoading.setVisibility(View.GONE);
                        loading_status.setVisibility(View.VISIBLE);
                        loading_status.setText(getString(R.string.reclick_loading));
                    }
                });
    }
}
